package com.epalmpay.commom;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.MenuInDTO;
import com.epalmpay.dto.system.MenuOutDTO;
import com.epalmpay.entity.Menu;
import com.epalmpay.service.management.IButtonService;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.service.management.IRoleService;
import com.epalmpay.service.management.IUserService;

@Controller
@RequestMapping(value="/manager/menu")
public class MenuController extends BaseController {
     
	@Resource  
    private IRoleService roleService;  
	@Resource  
    private IUserService userService;  
	@Resource  
    private IMenuService menuService;  
	@Resource  
    private IButtonService buttonService;  
	
	
	/**
	 * 显示菜单列表ztree(菜单管理)
	 * 
	 */
	@RequestMapping(value="/listAllMenu")
	@ResponseBody
	public List<Menu>  listAllMenuQx(MenuInDTO menuInDTO){
		try {  
			List<Menu> listMenu=menuService.listAllMenu(menuInDTO.getMenuId().toString(),menuInDTO.getMenuFlag());
			return listMenu;
		} catch (Exception e) {   
			e.printStackTrace();
		}
		return null;
	}	
		
		
	/**
	 * 显示菜单列表
	 * 
	 */
	@RequestMapping(value="/listMenu")
	@ResponseBody
	public MenuOutDTO  listMenu(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		ArrayList<MenuOutDTO> buttonList=new ArrayList<MenuOutDTO>();
		try {  
			List<Menu> listMenu=menuService.listSubMenuByParentId(menuInDTO.getMenuId()+"",menuInDTO.getMenuFlag());
			for (int i = 0; i < listMenu.size(); i++) {
				MenuOutDTO menuOut=new MenuOutDTO();
				menuOut.setMenuId(listMenu.get(i).getMenuId());
				menuOut.setMenuName(listMenu.get(i).getMenuName());
				menuOut.setMenuUrl(listMenu.get(i).getMenuUrl());
				menuOut.setParentId(listMenu.get(i).getParentId());
				menuOut.setMenuOrder(listMenu.get(i).getMenuOrder());
				menuOut.setMenuIcon(listMenu.get(i).getMenuIcon());
				menuOut.setMenuType(listMenu.get(i).getMenuType());
				menuOut.setMenuState(listMenu.get(i).getMenuState());
				menuOut.setTarget(listMenu.get(i).getTarget());
				buttonList.add(menuOut);
				menuOutDTO.setSubMenu(buttonList);
			}
			Menu  menu=	menuService.getMenuById(menuInDTO.getMenuId());
			if(menu!=null){
				menuOutDTO.setMenuId(menu.getMenuId());
				menuOutDTO.setMenuName(menu.getMenuName());
				menuOutDTO.setMenuUrl(menu.getMenuUrl());
				menuOutDTO.setParentId(menu.getParentId());
				menuOutDTO.setMenuOrder(menu.getMenuOrder());
				menuOutDTO.setMenuIcon(menu.getMenuIcon());
				menuOutDTO.setMenuType(menu.getMenuType());
				menuOutDTO.setMenuState(menu.getMenuState());
				menuOutDTO.setTarget(menu.getTarget());
			}
			menuOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return menuOutDTO;
		} catch (Exception e) { 
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}		
		
	
	/**
	 * 根据menuId查询详情
	 * 
	 */
	@RequestMapping(value="/MenuById")
	@ResponseBody
	public MenuOutDTO  menuById(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		try {  
			Menu  menu=	menuService.getMenuById(menuInDTO.getMenuId());
			if(menu!=null){
				menuOutDTO.setMenuId(menu.getMenuId());
				menuOutDTO.setMenuName(menu.getMenuName());
				menuOutDTO.setMenuUrl(menu.getMenuUrl());
				menuOutDTO.setParentId(menu.getParentId());
				menuOutDTO.setMenuOrder(menu.getMenuOrder());
				menuOutDTO.setMenuIcon(menu.getMenuIcon());
				menuOutDTO.setMenuType(menu.getMenuType());
				menuOutDTO.setMenuState(menu.getMenuState());
				menuOutDTO.setTarget(menu.getTarget());
			}
			menuOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return menuOutDTO;
		} catch (Exception e) { 
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}	
	
	
	/**
	 * 根据menuId修改菜单
	 * 
	 */
	@RequestMapping(value="/updateMenuId")
	@ResponseBody
	public MenuOutDTO  updateMenuId(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		try {  
			Menu menu=menuService.getMenuById(menuInDTO.getMenuId());
			if(menu!=null){
				menu.setParentId(menuInDTO.getParentId());
				menu.setMenuName(menuInDTO.getMenuName());
				menu.setMenuUrl(menuInDTO.getMenuUrl());
				menu.setMenuOrder(menuInDTO.getMenuOrder()); 
				menu.setMenuState(menuInDTO.getMenuState());
				menu.setMenuType(menuInDTO.getMenuType());
				menu.setMenuId(menuInDTO.getMenuId());
				if(menuInDTO.getMenuFlag()!=null){
					menu.setMenuFlag(menuInDTO.getMenuFlag()); 
				}
				menuService.edit(menu); 
				menuOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return menuOutDTO;
			}else{
				menuOutDTO.setErrorCode(Rescode.FAIL, "不存在该数据");
				return menuOutDTO;
			}
		} catch (Exception e) { 
			e.printStackTrace();
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	/**
	 * 根据menuId添加菜单
	 * 
	 */
	@RequestMapping(value="/saveMenuId")
	@ResponseBody
	public MenuOutDTO  saveMenuId(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		try {  
			Menu maxMenu=menuService.findMaxId(); 
			Menu menu=new Menu();
			menu.setParentId(menuInDTO.getParentId());
			menu.setMenuName(menuInDTO.getMenuName());
			menu.setMenuUrl(menuInDTO.getMenuUrl());
			menu.setMenuOrder(menuInDTO.getMenuOrder()); 
			menu.setMenuState(menuInDTO.getMenuState());
			menu.setMenuType(menuInDTO.getMenuType());
			menu.setMenuIcon(menuInDTO.getMenuIcon());
			menu.setMenuFlag(menuInDTO.getMenuFlag()); 
			menu.setMenuId(maxMenu.getMenuId()+1); 
			menuService.saveMenu(menu); 
			menuOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return menuOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	
	/**
	 * 根据menuId修改菜单图标
	 * 
	 */
	@RequestMapping(value="/updateMenuIcon")
	@ResponseBody
	public MenuOutDTO  updateMenuIcon(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		try {  
			Menu menu=new Menu();
			menu.setMenuIcon(menuInDTO.getMenuIcon()); 
			menu.setMenuId(menuInDTO.getMenuId());
			menuService.editicon(menu); 
			menuOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return menuOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	
	/**
	 * 根据menuId修改菜单图标
	 * 
	 */
	@RequestMapping(value="/deleteMenu")
	@ResponseBody
	public MenuOutDTO  deleteMenu(MenuInDTO menuInDTO){
		MenuOutDTO menuOutDTO=new MenuOutDTO();
		try {  
			List<Menu> listMenu=menuService.listSubMenuByParentId(menuInDTO.getMenuId()+"",menuInDTO.getMenuFlag());//判断是否有子菜单，是：不允许删除
			if(listMenu.size()>0){
				menuOutDTO.setErrorCode(Rescode.FAIL, "有子菜单,暂不能删除");
			}else{
				menuService.deleteMenuById(menuInDTO.getMenuId());
				menuOutDTO.setErrorCode(Rescode.SUCCESS, "删除成功");
			}
			return menuOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			menuOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
}
